/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.CommandPrompt;

import CrlException.CrlException;
import Data.ActivityLog.StatisticUtility;
/**
 *
 * @author CUHUUHOANG
 */
public class CmdPromptStatisticCtrl extends Thread {
    boolean isSuspended;
    boolean isStopped;
    int numLog=0;
    
        @Override
    public void run() {
        while (!isStopped) {
            try {
                checkTheadSuspend();
                numLog++;
                System.out.println(StatisticUtility.getLog());
                System.out.println(getStatistic());
                System.out.println("\t1. Pause | 2. Stop ");
                Thread.currentThread().sleep(350);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }


        }
    }
    
    public String getStatistic() throws CrlException{
        String out=null;
        try {
            out= "[URLQueued: "+StatisticUtility.getURLQueued()+"- URLProcessed: "
                    +StatisticUtility.getURLProcessed()+"- ThreadNum: "+StatisticUtility.getThreadRunning()+
                    "- Time: "+StatisticUtility.getTime()+"]";
            
        } catch (CrlException ex) {
            throw new CrlException(ex.getMessage());
        }
        return out;
    }
    public CmdPromptStatisticCtrl(){
        isSuspended = false;
        isStopped = false;
    }    
        
    synchronized public void suspendWork() throws CrlException {
        try {
            if (isSuspended) {
                isSuspended = false;
            } else {
                isSuspended = true;
            }
            notifyAll();
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }
    }

    synchronized public void StopThead() throws CrlException {
        try {
            this.isStopped = true;
            this.isSuspended = false;
            notifyAll();
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }

    }
    
    synchronized public void checkTheadSuspend() throws CrlException {
        try {
            if (isSuspended) 
                    wait();
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }
    }
        
}
